/*
*  逻辑放到Timer里面，每执行一次，计数器（count）加一
*/
var Logic = 
{
    count : 1,
    playerAimation(){
        var it = this;
        //  RYU1
        var RYU1 = Player.RYU1,skill = RYU1.skill[RYU1.stat];
        if(!(it.count % skill.t)){
            if(RYU1.stat !='jump'){
                skill.i++;
                if(skill.i >= skill.map[2]){
                    skill.i = 0;
                    if(skill.st) skill.st = 0;
                }
            }
            
            it.playerPosMove("RYU1");
            
        }
        
        var RYU2 = Player.RYU2,skill2 = RYU2.skill[RYU2.stat];
        if(!(it.count % skill2.t)){
            if(RYU2.stat !='jump'){
                skill2.i++;
                if(skill2.i >= skill2.map[2]){
                    skill2.i = 0;
                    if(skill2.st) skill2.st = 0;
                }
            }
            
            it.playerPosMove("RYU2");
        }
        
    },

    // 人物位置变化及校正
    playerPosMove(player){
        var it = this;
        var p = Player[player],skill = p.skill[p.stat];
        switch(p.stat){
            case "wait":
                break;
            case "goForward":
                if(p.faceTo == "left"){
                    p.pos[0]-=skill.step;
                }else{
                    p.pos[0]+=skill.step;
                }
                break;
            case "goBack":
                if(p.faceTo == "left"){
                    p.pos[0]+=skill.step;
                }else{
                    p.pos[0]-=skill.step;
                }
                break;
            case 'jump':
                // 需要对图像和位置处理
                
                if(!skill.st){
                    skill.st = (+new Date);
                }
                if((+new Date) - skill.st < skill.tn/2){
                    skill.i = 0;
                    p.pos[1] = Easing.easeOutQuad(0,(+new Date) - skill.st,0,100,skill.tn/2);
                }else{
                    skill.i = 1;
                    p.pos[1] = Easing.easeInQuad(0,(+new Date) - skill.st-(skill.tn/2),100,-100,skill.tn/2);
                }
                if((+new Date) - skill.st >= skill.tn){
                    it.playerAct_free(player);
                }
                
                break;
        }

        // 人物位置校正
        if(p.pos[0]<10) p.pos[0] = 10;
        if(p.pos[0]>w-10-62) p.pos[0] = w-10-62;
        if(p.pos[1]<0) p.pos[1] = 0;
    },

    
    // 切回等待状态，之前的状态及时未执行完也调成初始值
    playerAct_free(player){
        var p = Player[player],skill = p.skill[p.stat];
        if(p.stat != "wait"){
            skill.i = 0;
            p.stat = "wait";
            if(skill.st) skill.st = 0;
        }
        
    },


    
    // 获取人物的当前状态（非攻击状态下可以操作，攻击状态下需要等攻击动作完成）
    playerStat(player){
        return Player[player].stat;
    },


    
    // 获取人物的面部朝向
    playerFaceto(player){
        return Player[player].faceTo;
    },

    
    // 判断人物当前的是否为攻击状态
    isPlayerAttack(player){
        var p = Player[player],skill = p.skill[p.stat];
        return skill.ATK>0;
    },
    playerAct(actList,player){
        var it = this;
        if(it.isPlayerAttack(player)) return;
        switch(actList.length){
            case 0:
                var actListPress = Event.getPressKey();
                if(actListPress.length){
                    it.playerAct(actListPress,player);
                }else{
                    it.playerAct_free(player);
                }
                
                break;
            case 1:
                it.playerAct_one(actList[0],player);
                break;
            case 2:
                break;
            case 3: 
                break;
            case 4: 
                break;
        }  
    },
    playerAct_one(act,player){
        var it = this;
        var faceTo = it.playerFaceto(player);
        switch(act){
            case "jump":
                Player[player].stat = "jump";
                break;
            case "down":
                Player[player].stat = "down";
                break;
            case "left":
                if(faceTo == "right"){
                    Player[player].stat = "goBack";
                }else{
                    Player[player].stat = "goForward";
                }
                break;
            case "right":
                if(faceTo == "right"){
                    Player[player].stat = "goForward";
                }else{
                    Player[player].stat = "goBack";
                }
                break;
            
        }
    },
    event(){
        var it = this;
        var eList = {},kNO = 0,actList=[];
        Event.updateEvent();
        // 将之前的按键处理分析，组成动作
        for(var x in Event.eventList){
            var actArr = Event.eventList[x];
            if(!eList[actArr.name]) {
                eList[actArr.name] = actArr.addtime;
                
            }
        }
        for(var x in eList){
            actList[kNO++] = x;
            if(kNO>=4) break; 
        }
        it.playerAct(actList,"RYU2");
        
    },
    run(){
        var it = this;
        
        it.event();
        it.playerAimation();
        it.count++;
    }
        
};
